package com.xrui.hbase.codec;

import com.google.common.base.Preconditions;
import com.moz.kiji.delegation.Lookups;
import com.moz.kiji.delegation.NamedProvider;
import com.xrui.hbase.DecodedCell;

import java.io.IOException;


/**
 * Interface for cell decoders.
 * <p>
 * A cell decoder is specific to one column and decodes only one type of value.
 * HBaseCell decoders are instantiated via {@link DefaultCellDecoderFactory}.
 *
 * @param <T> Type of the values being decoded.
 */
public interface CellDecoder<T> extends NamedProvider {
    /**
     * Decodes a cell from its binary-encoded form.
     *
     * @param bytes Binary encoded cell.
     * @return the decoded HBaseCell.
     * @throws IOException on I/O error.
     */
    DecodedCell<T> decodeCell(byte[] bytes) throws IOException;

    /**
     * Decodes a cell from its binary-encoded form.
     *
     * @param bytes Binary encoded cell value.
     * @return the decoded cell value.
     * @throws IOException on I/O error.
     */
    T decodeValue(byte[] bytes) throws IOException;

    class Factory {
        /**
         * Creates a new CellDecoder with specified decoder provider name
         *
         * @param name of the decoder
         * @return new instance of the decoder
         */
        public static CellDecoder create(String name) {
            Preconditions.checkNotNull(name);
            return Lookups.getNamed(CellDecoder.class).lookup(name);
        }
    }
}
